// pages/task_detail/task_detail.js
import Toast from '../../miniprogram_npm/@vant/weapp/dist/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dist/dialog/dialog';

const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        _id: "",
        task: {},
        openid: "",
        contact_options:[
            { text: '微信号', value: 0 },
          { text: 'QQ号', value: 1 },
          { text: '手机号', value: 2 }
        ],
        contact_index:0,
        contactNumber:"",
        show:false
    },

    taskCancelPublish() {
        wx.cloud.callFunction({
            name: "changeTaskData",
            data: {
                action: "taskCancelPublish",
                _id: this.data.task._id,
                openid: this.data.openid
            },
            success: (res) => {
                console.log(res)
                wx.navigateBack({
                    delta: 0,
                })
                wx.showToast({
                    title: '任务取消成功',
                })
            },
            fail: (res) => {
                console.log(res)
                wx.showToast({
                    icon: "none",
                    title: '网络不给力…',
                })
            }
        })
    },

    taskComplete() {
        wx.cloud.callFunction({
            name: "changeTaskData",
            data: {
                action: "taskCompleted",
                _id: this.data.task._id,
                openid: this.data.openid
            },
            success: (res) => {
                console.log("任务完成")
                console.log(res)
                wx.showToast({
                    title: '任务已完成',
                })
                this.onShow()
            },
            fail: (res) => {
                console.log(res)
                wx.showToast({
                    icon: "none",
                    title: '网络不给力…',
                })
            }
        })
    },

    taskReceived() {
        this.setData({
            show:false
        })
        wx.cloud.callFunction({
            name:"changeTaskData",
            data:{
                action:"taskReceived",
                _id:this.data.task._id,
                openid:this.data.openid,
                contactType:this.data.contact_options[this.data.contact_index].text,
                contactNumber:this.data.contactNumber
            },
            success:(res)=>{
                console.log(res)
                wx.showToast({
                    title: '任务领取成功',
                })
                this.onShow()             
            },
            fail:(res)=>{
                console.log(res)
                wx.showToast({
                    icon:"none",
                    title: '网络不给力…',
                })
            }
        })
    },

    taskCancelReceive() {
        wx.cloud.callFunction({
            name: "changeTaskData",
            data: {
                action: "taskCancelReceive",
                _id: this.data.task._id,
                openid: this.data.openid,
            },
            success: (res) => {
                console.log(res)
                wx.showToast({
                    title: '任务放弃成功',
                })
                this.onShow()
            },
            fail: (res) => {
                console.log(res)
                wx.showToast({
                    icon: "none",
                    title: '网络不给力…',
                })
            }
        })
    },

    showPopup() {
        this.setData({ show: true });
      },
    
     onClose() {
        this.setData({
             show: false ,
             contact_index:0,
             contactNumber:""
        });
      },

      contactTypeChange(e){
        this.data.contact_index=e.detail
    },

    contactWithPublisher(){
        Dialog.alert({
            title:"联系发布人",
            message:this.data.task.publisherInfo.contactType+": "+this.data.task.publisherInfo.contactNumber
        }).then(() => {
            // on close
          });
    },

    contactWithReceiver(){
        Dialog.alert({
            title:"联系领取人",
            message:this.data.task.receiverInfo.contactType+": "+this.data.task.receiverInfo.contactNumber
        }).then(() => {
            // on close
          });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            _id: options.data
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.cloud.callFunction({
            name: "getTaskData",
            data: {
                action: "getAll",
                _id: this.data._id,
            },
            success: (res) => {
                console.log(res)
                this.setData({
                    task: res.result,
                    openid: app.globalData.openid
                })
            },
            fail: (res) => {
                console.log(res)
                wx.showToast({
                    title: '网络不给力…',
                })
            }
        })
    },



    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})